/*
 *  Copyright (c) 2018 Oray Inc. All rights reserverd.
 *  No Part of this file may be reproduced, stored
 *  in a retrieval system, or transmitted, in any form, or by any means,
 *  electronic, mechanical, photocopying, recording, or otherwise,
 *  without the prior consent of Oray Inc.
 *
 *  Author: qianwei
 *  Create on:  19-3-6 下午4:54
 *  FileName:BluetoothUtils.java
 *
 *
 */

package com.bnq.control.desktop.utils;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;

import com.oray.commonutils.utils.LogUtil;
import com.oray.sunlogin.manager.util.PermissionUtils;

import java.util.List;
import java.util.Set;

public class BluetoothUtils {

    private static final String TAG = BluetoothUtils.class.getSimpleName();
    public static boolean isConnectedBluetoothKeyboard = false;

    /**
     * 初始化蓝牙
     *
     * @param context Context
     */
    public static void initBluetoothAdapterStatus(Context context) {
        isConnectedBluetoothKeyboard = false;
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (null != bluetoothAdapter && !bluetoothAdapter.isEnabled()) return;
        if (Build.VERSION.SDK_INT > 29) {        //蓝牙未开启
            foundConnectedBoundDevice();
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (PermissionUtils.hasPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)) {
                getProfileProxy(context);
            } else {
                foundConnectedBoundDevice();
            }
        } else {
            getProfileProxy(context);
        }
    }

    /**
     * 获取输入类型蓝牙设备 需要定位权限
     * Android11 getConnectedDevices 已改为系统Api暂时不支持调用
     * TODO Android12 需要特殊适配
     *
     * @see android.bluetooth.BluetoothHidHost
     */
    private static void getProfileProxy(Context context) {
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (null != bluetoothAdapter) {
            bluetoothAdapter.getProfileProxy(context, new BluetoothProfile.ServiceListener() {
                @Override
                public void onServiceConnected(int profile, BluetoothProfile proxy) {
                    LogUtil.i(TAG, "onServiceConnected>>>");
                    try {
                        if (null != proxy) {
                            List<BluetoothDevice> devices = proxy.getConnectedDevices();
                            isConnectedBluetoothKeyboard = null != devices && devices.size() > 0;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        LogUtil.i(TAG, "getBluetoothInfo>>> exception" + e.getMessage());
                        foundConnectedBoundDevice();
                    }
                }

                @Override
                public void onServiceDisconnected(int profile) {
                    LogUtil.i(TAG, "onServiceDisconnected>>>");
                }
            }, 4);  // 4即 BluetoothProfile.INPUT_DEVICE的值
        }
    }

    /**
     * 获取当前绑定设备
     */
    private static void foundConnectedBoundDevice() {
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (null != bluetoothAdapter) {
            Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
            if (null != bondedDevices && bondedDevices.size() > 0) {
                for (BluetoothDevice d : bondedDevices) {
                    if (null != d && hasKeyBoardDevice(d.getName())) {
                        isConnectedBluetoothKeyboard = true;
                        break;
                    }
                }
            }
        }
    }

    /**
     * 是否连接蓝牙键盘
     *
     * @param name 键盘设备名称
     * @return true 有键盘设备 但是这种方式不准确
     */
    private static boolean hasKeyBoardDevice(String name) {
        return !TextUtils.isEmpty(name) && name.toLowerCase().contains("keyboard");
    }
}
